package com.sun.lib.base;

import android.os.Bundle;

import androidx.annotation.Nullable;

import com.sun.lib.base.basemvp.BaseMvpView;
import com.sun.lib.base.basemvp.BasePresenter;
import com.sun.lib.base.basemvp.Presenter;
import com.sun.lib.inject.InjectPresenter;
import com.sun.lib.util.ToastUtil;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/*
 * Created by Administrator on 2019/7/24.
 *
 * #                                                   #
 * #                       _oo0oo_                     #
 * #                      o8888888o                    #
 * #                      88" . "88                    #
 * #                      (| -_- |)                    #
 * #                      0\  =  /0                    #
 * #                    ___/`---'\___                  #
 * #                  .' \\|     |# '.                 #
 * #                 / \\|||  :  |||# \                #
 * #                / _||||| -:- |||||- \              #
 * #               |   | \\\  -  #/ |   |              #
 * #               | \_|  ''\---/''  |_/ |             #
 * #               \  .-\__  '-'  ___/-. /             #
 * #             ___'. .'  /--.--\  `. .'___           #
 * #          ."" '<  `.___\_<|>_/___.' >' "".         #
 * #         | | :  `- \`.;`\ _ /`;.`/ - ` : | |       #
 * #         \  \ `_.   \_ __\ /__ _/   .-` /  /       #
 * #     =====`-.____`.___ \_____/___.-`___.-'=====    #
 * #                       `=---='                     #
 * #     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~   #
 * #                                                   #
 * #               佛祖保佑         永无BUG              #
 * #                                                   #
 */
public abstract class BaseMvpActivity<T extends Presenter> extends BaseActivity implements BaseMvpView {
    private T mPresenter;
    private List<BasePresenter> mInjectPresenters;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        this.mPresenter = createPresenter();
        if (this.mPresenter != null)
            this.mPresenter.attachView(this);
        createInjectPresenters();
        super.onCreate(savedInstanceState);
    }

    private void createInjectPresenters() {
        mInjectPresenters = new ArrayList<>();
        Field[] fields = this.getClass().getDeclaredFields();
        for (Field field : fields) {
            InjectPresenter injectPresenter = field.getAnnotation(InjectPresenter.class);
            if (injectPresenter != null) {
                try {
                    Class<? extends BasePresenter> type = (Class<? extends BasePresenter>) field.getType();
                    BasePresenter mInjectPresenter = type.newInstance();
                    mInjectPresenter.attachView(this);
                    field.setAccessible(true);
                    field.set(this, mInjectPresenter);
                    mInjectPresenters.add(mInjectPresenter);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (ClassCastException e) {
                    e.printStackTrace();
                    throw new RuntimeException("subClass must extends Class:BasePresenter");
                }
            }
        }
    }

    /**
     * 创建presenter
     *
     * @return
     */
    protected abstract T createPresenter();

    @Override
    protected boolean isBindEventBus() {
        return false;
    }

    @Override
    protected void getExtraEvent(Bundle extras) {

    }

    @Override
    public void showLoadingDialog(String msg) {

    }

    @Override
    public void dismissLoadingDialog() {

    }

    @Override
    public void showToast(String message) {
        ToastUtil.show(context, message);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        this.mPresenter.detachView();
    }
}
